/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Editor.java
 *
 * Created on May 4, 2011, 8:21:34 PM
 */
package framework.panes;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Darlan
 */
public class Editor extends JDialog
{
    private boolean accepted = false;
    private EditorPane editor;
    
    /**
     * 
     * @param parent
     * @param editor
     * @param title
     * @return 
     */
    public static boolean edit( Frame parent, EditorPane editor, String title )
    {
        Editor edit = new Editor( parent, editor );
        edit.setTitle( title );
        
        edit.setVisible( true );
        
        return edit.accepted();
    }

    /**
     * 
     * @param parent
     * @param editor 
     */
    private Editor( Frame parent, EditorPane editor )
    {
        super( parent, true );
        initComponents();
        this.editor = editor;
        setSize( 600, 500 );
        setLocationRelativeTo( parent );
//        addKeyListener( new KeyAdapter() 
//        {
//            @Override
//            public void keyTyped( KeyEvent e )
//            {
//                if ( e.getKeyCode() == KeyEvent.VK_ESCAPE )
//                {
//                    
//                }
//            }
//        } );
        
        editorPane.add( editor, new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0, 
                GridBagConstraints.WEST, GridBagConstraints.BOTH, 
                new Insets( 0, 0, 0, 0 ), 0, 0 ) );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        editorPane = new javax.swing.JPanel();
        buttonsPane = new javax.swing.JPanel();
        btOK = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        setLayout(new java.awt.GridBagLayout());

        editorPane.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 15, 0, 15);
        add(editorPane, gridBagConstraints);

        buttonsPane.setLayout(new java.awt.GridBagLayout());

        btOK.setText("OK");
        btOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOKActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 15, 10);
        buttonsPane.add(btOK, gridBagConstraints);

        btCancel.setText("Cancelar");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 15, 15);
        buttonsPane.add(btCancel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(buttonsPane, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible( false );
        dispose();
    }//GEN-LAST:event_closeDialog

    private void btOKActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btOKActionPerformed
    {//GEN-HEADEREND:event_btOKActionPerformed
        String error = editor.validateInput();
        
        if ( error.isEmpty() )
        {
            accepted = true;
            closeDialog( null );
        }
        
        else
        {
            JOptionPane.showMessageDialog( this, error, "Guest", JOptionPane.PLAIN_MESSAGE );
        }
    }//GEN-LAST:event_btOKActionPerformed

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btCancelActionPerformed
    {//GEN-HEADEREND:event_btCancelActionPerformed
        accepted = false;
        closeDialog( null );
    }//GEN-LAST:event_btCancelActionPerformed
    
    /**
     * 
     * @return 
     */
    private boolean accepted()
    {
        editor.accept();
        return accepted;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btOK;
    private javax.swing.JPanel buttonsPane;
    private javax.swing.JPanel editorPane;
    // End of variables declaration//GEN-END:variables
}
